using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Divelements.WizardFramework;

namespace SendHTML
{
    public partial class FormMain : Form
    {
        const int MIN_EMAIL_LEN = 5;
        MailInfo mail = new MailInfo();

        public FormMain()
        {
            InitializeComponent();

            //hook up wizard events
            wizard1.Cancel += new EventHandler(this.wizard1Cancel);
            wizard1.Finish += new EventHandler(this.wizard1Finish);

            PageServerDetails.BeforeMoveNext += new WizardPageEventHandler(this.PageServerDetails_BeforeMoveNext);
            PageFileDetails.BeforeMoveNext += new WizardPageEventHandler(this.PageFileDetails_BeforeMoveNext);
            PageRecipentDetails.BeforeMoveNext +=new WizardPageEventHandler(this.PageRecipentDetails_BeforeMoveNext);
            PageConfirmSend.BeforeMoveBack += new WizardPageEventHandler(this.PageConfirmSend_BeforeMoveBack);
            PageConfirmSend.BeforeMoveNext += new WizardPageEventHandler(this.PageConfirmSend_BeforeMoveNext);
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            mail.Load(); //get the data the user entered the last time a file was sent
            txtSMTPServer.Text = mail.SMTPServer;
            txtSMTPUser.Text = mail.SMTPUser;
            txtSMTPPassword.Text = mail.SMTPPassword;
            txtSMTPDomain.Text = mail.SMTPDomain;
            chkMustAuthenticate.Checked = mail.SMTPAuthenticate;
            txtLocalFile.Text = mail.FileName;
            txtSender.Text = mail.Sender;
            txtRecipientsTO.Text = mail.RecipientsTO;
            txtRecipientsCC.Text = mail.RecipientsCC;
            txtSubject.Text = mail.Subject;
            txtMessageBody.Text = mail.Message;
        }

        private void wizard1Cancel(object sender, System.EventArgs e)
        {
            Close();
        }

        private void wizard1Finish(object sender, System.EventArgs e)
        {
            Close();
        }


        // START PAGE

        private void PageStart_BeforeDisplay(object sender, EventArgs e)
        {

        }

        // STEP 1: SERVER

        private void PageServerDetails_BeforeDisplay(object sender, EventArgs e)
        {
            grpCredentials.Enabled = chkMustAuthenticate.Checked;
        }

        private void chkMustAuthenticate_CheckedChanged(object sender, EventArgs e)
        {
            grpCredentials.Enabled = chkMustAuthenticate.Checked;
        }

        private void PageServerDetails_BeforeMoveNext(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // don't move on unless we've got a server to contact
            mail.SMTPServer = txtSMTPServer.Text;
            mail.SMTPUser = txtSMTPUser.Text;
            mail.SMTPPassword = txtSMTPPassword.Text;
            mail.SMTPDomain = txtSMTPDomain.Text;
            mail.SMTPAuthenticate = chkMustAuthenticate.Checked;

            string errMsg = mail.CheckServer();
            if (errMsg != null)
            {
                lblServerErrorMsg.Visible = true;
                lblServerErrorMsg.Text = errMsg;
                e.Cancel = true;
            }
            else
                lblServerErrorMsg.Visible = false;
        }

        // STEP 2: HTML FILE

        private void PageFileDetails_BeforeDisplay(object sender, EventArgs e)
        {

        }

        private void butChooseFile_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter = "htm files (*.htm)|*.htm|html files (*.html)|*.html|All files (*.*)|*.*";
            openDialog.FilterIndex = 1;
            openDialog.RestoreDirectory = true;
            openDialog.Title = "Choose HTML file";

            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                txtLocalFile.Text = openDialog.FileName;
            }
        }

        private void PageFileDetails_BeforeMoveNext(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // don't move on unless file is valid
            mail.FileName = txtLocalFile.Text;
            string errMsg = mail.CheckFile();
            if (errMsg != null)
            {
                lblFileErrorMsg.Text = errMsg;
                lblFileErrorMsg.Visible = true;
                e.Cancel = true;
            }
            else
                lblFileErrorMsg.Visible = false;
        }

        // STEP 3: RECIPIENTS

        private void PageRecipentDetails_BeforeDisplay(object sender, EventArgs e)
        {
        }

        private void PageRecipentDetails_BeforeMoveNext(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // don't move on unless we have a valid sender and someone to send to
            string errMsg;

            mail.Sender = txtSender.Text;
            errMsg = mail.CheckSender();
            if (errMsg != null)
            {
                lblSenderErrorMsg.Text = errMsg;
                lblSenderErrorMsg.Visible = true;
                e.Cancel = true;
            }
            else
                lblSenderErrorMsg.Visible = false;

            mail.RecipientsTO = txtRecipientsTO.Text;
            mail.RecipientsCC = txtRecipientsCC.Text;
            errMsg = mail.CheckRecipients();
            if (errMsg != null)
            {
                lblRecipErrorMsg.Text = errMsg;
                lblRecipErrorMsg.Visible = true;
                e.Cancel = true;
            }
            else
                lblRecipErrorMsg.Visible = false;
        }


        // STEP 4: SUBJECT & MESSAGE

        private void PageMessageDetails_BeforeDisplay(object sender, EventArgs e)
        {

        }

        // STEP 5: CONFIRM & SEND

        private void PageConfirmSend_BeforeMoveBack(object sender, System.ComponentModel.CancelEventArgs e)
        {
            wizard1.NextText = "Next";  
        }

        private void PageConfirmSend_BeforeDisplay(object sender, EventArgs e)
        {
            wizard1.NextText = "Send";
            lblProblem.Visible = false;
            txtFeedback.Visible = false;
        }

        private void PageConfirmSend_BeforeMoveNext(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                // Here's where we try and send the file
                mail.Subject = txtSubject.Text;
                mail.Message = txtMessageBody.Text;
                mail.Save();
                mail.Send();
            }
            catch (Exception excep)
            {
                lblProblem.Visible = true;
                txtFeedback.Text = excep.Message;
                txtFeedback.Visible = true;
                e.Cancel = true;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        // STEP 6: FINISH

        private void PageFinished_BeforeDisplay(object sender, EventArgs e)
        {
            PageFinished.FinishText = "Sent " + txtLocalFile.Text;
            PageFinished.AllowCancel = false;
            PageFinished.AllowMovePrevious = false;
        }

    }
}